package in.co.codedoc.cg.velocity;

import in.co.codedoc.cg.CGAnnotationProcessor;

import java.io.IOException;
import java.io.Writer;

import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.parser.node.Node;

public class IfNthRoundDirective extends CGBlockDirective
{
	public IfNthRoundDirective()
	{
		super("Round number");
	}

	@Override
	public String getName()
	{
		return "IfNthRound";
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean render(InternalContextAdapter context, Writer writer,
			Node node) throws IOException, ResourceNotFoundException,
			ParseErrorException, MethodInvocationException
	{
		
		int desiredRound = Integer.parseInt(getArgument(context, node, 0));
		int currentRound = CGAnnotationProcessor.getRoundNumber();
		
		if(desiredRound == currentRound)
		{
			node.jjtGetChild(1).render(context, writer);
		}
		
		return true;
	}

}
